﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Tasker.Common.Web
{
    public sealed class HttpContextCollectionContainer<T> : ICollection<T>, IEnumerable<T>
    {
        private readonly HttpContextProperty<ICollection<T>> _container = new HttpContextProperty<ICollection<T>>();

        private IEnumerable<T> AsEnumerable
        {
            get
            {
                if (!_container.HasValue)
                    _container.Value = new List<T>();
                return _container.Value;
            }
        }

        private ICollection<T> AsCollection
        {
            get
            {
                if (!_container.HasValue)
                    _container.Value = new List<T>();
                return _container.Value;
            }
        }

        public void Add(T item)
        {
            AsCollection.Add(item);
        }

        public void Clear()
        {
            AsCollection.Clear();
        }

        public bool Contains(T item)
        {
            return AsCollection.Contains(item);
        }

        public void CopyTo(T[] array, int arrayIndex)
        {
            AsCollection.CopyTo(array, arrayIndex);
        }

        public int Count
        {
            get { return AsCollection.Count; }
        }

        public bool IsReadOnly
        {
            get { return AsCollection.IsReadOnly; }
        }

        public bool Remove(T item)
        {
            return AsCollection.Remove(item);
        }

        public IEnumerator<T> GetEnumerator()
        {
            return AsCollection.GetEnumerator();
        }

        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            return AsCollection.GetEnumerator();
        }
    }
}
